package com.winit.schedule.task.tracker.utils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.winit.schedule.core.repository.constants.TaskRunnerStatus;
import com.winit.schedule.core.repository.entity.TaskItemEntity;
import com.winit.schedule.core.repository.entity.TaskRunnerEntity;
import com.winit.schedule.task.tracker.runner.AbstractTaskManager;

public class DataConverter {

    public static TaskRunnerEntity convert2TaskTunner(AbstractTaskManager taskManager) {
        TaskRunnerEntity taskRunnerEntity = new TaskRunnerEntity();
        taskRunnerEntity.setTaskTrackerUuid(taskManager.getTaskTracker().getUuid());
        taskRunnerEntity.setTaskId(taskManager.getTask().getTaskId());
        taskRunnerEntity.setTaskItemIds(getTaskItemIdSet(taskManager.getTask().getTaskItems()));
        taskRunnerEntity.setTaskRunnerUuid(taskManager.getTaskRunnerUuid());
        taskRunnerEntity.setRegisterTime(taskManager.getRegisterTime());
        taskRunnerEntity.setThreadNum(taskManager.getThreadNum());
        taskRunnerEntity.setLastFetchDataTime(taskManager.getStatisticsInfo().getLastFetchDataTime());
        taskRunnerEntity.setDealDataSucess(taskManager.getStatisticsInfo().getDealDataSucess().longValue());
        taskRunnerEntity.setDealDataFail(taskManager.getStatisticsInfo().getDealDataFail().longValue());
        taskRunnerEntity.setFetchDataNum(taskManager.getStatisticsInfo().getFetchDataNum().longValue());
        taskRunnerEntity.setFetchDataCount(taskManager.getStatisticsInfo().getFetchDataCount().longValue());
        taskRunnerEntity.setDealSpendTime(taskManager.getStatisticsInfo().getDealSpendTime().longValue());
        taskRunnerEntity.setStatus(taskManager.isSleeping() == true ? TaskRunnerStatus.SLEEP : TaskRunnerStatus.RUNNING);

        return taskRunnerEntity;
    }

    private static Set<String> getTaskItemIdSet(List<TaskItemEntity> taskItems) {
        Set<String> itemIdSet = new HashSet<String>();
        for (TaskItemEntity item : taskItems) {
            itemIdSet.add(item.getTaskItemId());
        }
        return itemIdSet;

    }

}
